package com.stormrage.edu.serv.learn.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.learn.LeaBunchDao;
import com.stormrage.edu.dao.learn.LeaStudentDao;
import com.stormrage.edu.dao.student.StuAcctDao;
import com.stormrage.edu.dao.student.StuInfoDao;
import com.stormrage.edu.serv.ent.EntEnterpriseScoreServ;
import com.stormrage.edu.serv.exam.ExmScoreServ;
import com.stormrage.edu.serv.learn.LeaBunchServ;
import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.entity.ent.EntEnterpriseScore;
import com.stormrage.entity.exam.ExmScore;
import com.stormrage.entity.exam.excel.ExmOperExcel;
import com.stormrage.entity.exam.excel.ExmUsuaExcel;
import com.stormrage.entity.excel.StudentExcel;
import com.stormrage.entity.learn.LeaBunch;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.rep.LeaBunchRep;
import com.stormrage.entity.learn.req.LeaBunchReq;
import com.stormrage.entity.learn.req.LeaStudentReq;
import com.stormrage.entity.rep.ListRep;
import com.stormrage.entity.student.StuAcct;
import com.stormrage.entity.student.StuInfo;
import com.stormrage.entity.student.req.StuAcctReq;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.ExcelUtil;

/**
 * @Title: LeaClassServImpl.java
 * @Package com.stormrage.edu.serv.learn.impl
 * @Description: 班级 Service
 * @author www.stormragetech.com
 * @date 2016年4月13日
 * @version V1.0
 */
@Service
public class LeaBunchServImpl implements LeaBunchServ {

	private static final Logger logger = LoggerFactory.getLogger(LeaBunchServImpl.class);

	@Autowired
	private LeaBunchDao leaBunchDao;

	@Autowired
	private LeaStudentDao leaStudentDao;

	@Autowired
	private StuAcctDao stuAcctDao;

	@Autowired
	private StuInfoDao stuInfoDao;

	@Autowired
	private EntEnterpriseScoreServ entEnterpriseScoreServ;

	@Autowired
	private ExmScoreServ exmScoreServ;

	@Autowired
	private LeaStudentServ leaStudentServ;

	@Override
	public void addStu(final StuInfo info, final Integer bunchId) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 addStu 】- 开始运行 ");

		// ------------------ 创建学员账户 ------------------
		StuAcct acct = StuAcct.instance();
		Integer acctId = stuAcctDao.create(acct);
		acct = StuAcct.instance(acct, acctId);
		stuAcctDao.update(acct);

		// ------------------ 创建学员信息 ------------------
		info.setAcctId(acctId);
		stuInfoDao.create(info);

		// ------------------ 报名 ------------------
		LeaStudent stu = LeaStudent.instance(acctId, bunchId, new Date(), 1);
		leaStudentDao.create(stu);

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 addStu 】- 结束运行 ");

	}

	@Override
	public Integer queryCountByTermEnt(final LeaBunchReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryCountByTermEnt 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("termId", arg0.getTermId());
		map.put("entId", arg0.getEntId());

		Integer count = leaBunchDao.queryCountByTermEnt(map);

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryCountByTermEnt 】- 结束运行 ");
		return count;

	}

	@Override
	public List<LeaBunch> queryByTermEnt(final LeaBunchReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryByTermEnt 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("termId", arg0.getTermId());
		map.put("entId", arg0.getEntId());

		logger.debug(" 请求参数为 >>>>>>> " + map.toString());

		List<LeaBunch> array = leaBunchDao.queryByTermEnt(map);

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryByTermEnt 】- 结束运行 ");
		return array;

	}

	@Override
	public List<LeaBunchRep> queryList(final LeaBunchReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("entId", arg0.getEntId());
		map.put("locked", arg0.getLocked());

		Integer upload = null;
		EntEnterpriseScore entScore = entEnterpriseScoreServ.queryByEntId(arg0.getEntId());
		if (null != entScore)
			upload = entScore.getLocked();

		List<LeaBunchRep> array = null;

		if (arg0.getLocked().equals(1))
			array = leaBunchDao.queryList(map);
		if (arg0.getLocked().equals(0))
			array = leaBunchDao.queryListForLearn(map);

		int seq = 1;
		Iterator<LeaBunchRep> it = array.iterator();
		while (it.hasNext()) {
			LeaBunchRep entity = it.next();
			Integer count = 0;

			if (arg0.getLocked().equals(1)) {
				List<StuAcctReq> list = ExcelUtil.importExcel(arg0.getPath() + entity.getExcel(), StuAcctReq.class);
				count = list.size();
			}
			if (arg0.getLocked().equals(0)) {
				LeaStudentReq req = LeaStudentReq.instance(entity.getId(), 0, null);
				count = leaStudentServ.queryCountByBunchLock(req);
			}

			entity = LeaBunchRep.instance(entity, seq, count, upload);
		}

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 QueryList 】- 结束运行 ");
		return array;

	}

	@Override
	public List<LeaBunchRep> queryListForApply(final LeaBunchReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("entId", arg0.getEntId());

		Integer upload = null;
		EntEnterpriseScore entScore = entEnterpriseScoreServ.queryByEntId(arg0.getEntId());
		if (null != entScore)
			upload = entScore.getLocked();

		List<LeaBunchRep> array = leaBunchDao.queryListForApply(map);

		int seq = 1;
		Iterator<LeaBunchRep> it = array.iterator();
		while (it.hasNext()) {
			LeaBunchRep entity = it.next();

			List<StuAcctReq> list = ExcelUtil.importExcel(arg0.getPath() + entity.getExcel(), StuAcctReq.class);
			entity = LeaBunchRep.instance(entity, seq, list.size(), upload);
		}

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 QueryList 】- 结束运行 ");
		return array;

	}

	@Override
	public ListRep queryListForLearn(final LeaBunchReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 QueryListForLearn 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0.getEntId());

		Integer count = leaBunchDao.queryCountForLearn(map);

		List<LeaBunchRep> array = leaBunchDao.queryListForLearn(map);
		int seq = arg0.getPage().getStartPage() + 1;

		Iterator<LeaBunchRep> it = array.iterator();
		while (it.hasNext()) {
			LeaBunchRep entity = it.next();

			LeaStudentReq req = LeaStudentReq.instance(entity.getId(), 0, null);
			Integer stuCount = leaStudentServ.queryCountByBunchLock(req);

			entity = LeaBunchRep.instance(entity, seq, stuCount);
			seq++;
		}

		ListRep rep = ListRep.instance(count, array);

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 QueryListForLearn 】- 结束运行 ");
		return rep;

	}

	@Override
	public Workbook queryOperExcel(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryOperExcel 】-【 STARTED 】 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bunchId", arg0);

		List<ExmOperExcel> array = leaBunchDao.queryOperListForExcel(map);

		ExportParams params = ExcelUtil.initParams("实操成绩单", "sheet", false);
		Workbook workbook = ExcelUtil.exportExcel(array, ExmOperExcel.class, params);

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryOperExcel 】-【 COMPLETED 】 ");
		return workbook;

	}

	@Override
	public Workbook queryUsuaExcel(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryUsuaExcel 】-【 STARTED 】 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bunchId", arg0);

		List<ExmUsuaExcel> array = leaBunchDao.queryUsuaListForExcel(map);

		ExportParams params = ExcelUtil.initParams("平时成绩单", "sheet", false);
		Workbook workbook = ExcelUtil.exportExcel(array, ExmUsuaExcel.class, params);

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryUsuaExcel 】-【 COMPLETED 】 ");
		return workbook;
	}

	@Override
	public Workbook queryStudentExcel(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryOperExcel 】-【 STARTED 】 ");

		List<StudentExcel> array = leaStudentServ.queryListByBunchForExcel(arg0);

		ExportParams params = ExcelUtil.initParams("学员账户信息表", "sheet", false);
		Workbook workbook = ExcelUtil.exportExcel(array, StudentExcel.class, params);

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 queryOperExcel 】-【 COMPLETED 】 ");
		return workbook;

	}

	@Override
	public void uploadUsua(final MultipartFile arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 uploadUsua 】-【 STARTED 】 ");

		try {

			InputStream input = arg0.getInputStream();
			List<ExmUsuaExcel> array = ExcelUtil.importExcel(input, ExmUsuaExcel.class);

			Iterator<ExmUsuaExcel> it = array.iterator();
			while (it.hasNext()) {
				ExmUsuaExcel entity = it.next();
				ExmScore score = exmScoreServ.queryByStuId(entity.getId());
				score.setUsua(entity.getScore());
				exmScoreServ.update(score);
			}

		} catch (IOException e) {
			throw StormrageTechException.instance(ErrCons.SERV_EXP, ErrCons.SERV_EXP_MSG,
					StormrageTechException.ERR_SYS, "【 LeaBunchServ 】- 【 uploadUsua 】", e);
		}

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 uploadUsua 】-【 COMPLETED 】 ");

	}

	@Override
	public void uploadOper(final MultipartFile arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 uploadUsua 】-【 STARTED 】 ");

		try {

			InputStream input = arg0.getInputStream();
			List<ExmOperExcel> array = ExcelUtil.importExcel(input, ExmOperExcel.class);

			Iterator<ExmOperExcel> it = array.iterator();
			while (it.hasNext()) {
				ExmOperExcel entity = it.next();
				ExmScore score = exmScoreServ.queryByStuId(entity.getId());
				score.setOper(entity.getScore());
				exmScoreServ.update(score);
			}

		} catch (IOException e) {
			throw StormrageTechException.instance(ErrCons.SERV_EXP, ErrCons.SERV_EXP_MSG,
					StormrageTechException.ERR_SYS, "【 LeaBunchServ 】- 【 uploadUsua 】", e);
		}

		logger.debug(" 【 Serv 】- 【 LeaBunchServ 】- 【 uploadUsua 】-【 COMPLETED 】 ");

	}

}
